package com.tubi.maipiao;
//线程继承Runnble 接口
public class Ticket  implements Runnable {
    //票数
    public  int ticket = 100;
    public  Object obj = new Object();


    /*
    *
    *  同步代码块
    * synchronized(任意对象) {
            多条语句操作共享数据的代码
        }
    * */
    @Override
    public void run() {
        while (true){
            synchronized (obj){ //多个线程使用同一把锁
                if ( ticket <= 0){
                    break;
                }else{
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ticket--;
                    //Thread.currentThread().getName() 获取线程名称
                    System.out.println(Thread.currentThread().getName() + "在买票，还剩下"+ticket+'张');
                }
            }

        }
    }
}
